import {Injectable} from '@angular/core';
import {HttpService} from '@kylin/http';
import {ConfigService} from '@kylin/config';

@Injectable({
    providedIn: 'root'
})
export class FileManagerService {
    files: Array<any>;
    localFilesDate: Array<any>;
    isRemote: true; // 是否从服务获取数据，false:本地mock
    imgPrefix = '../../../assets/img/file-default';

    constructor(private http: HttpService, private configSvc: ConfigService) {
    }

    getLocalData() {
        if (this.isRemote) {
            return null;
        } else {
            return [
                {
                    projectName: '耕地',
                    fileName: 'test.html',
                    url: 'xxxx/safafd/adf.html',
                    contentType: 'html',
                    size: '1024',
                    createUser: 'hex1',
                    createTime: '1560494333075'
                },
                {
                    projectName: '草种质',
                    fileName: 'test.img',
                    contentType: 'img',
                    url: 'xxxx/safafd/adf.img',
                    size: '102456',
                    createUser: 'hex2',
                    createTime: '1560494333075'
                },
                {
                    projectName: '林地',
                    fileName: 'test.xls',
                    contentType: 'xls',
                    url: 'xxxx/safafd/adf.xls',
                    size: '1012324',
                    createUser: 'hex3',
                    createTime: '1560494333075'
                }
            ]
        }
    }

    getProjectNameOptions() {
        return [
            {label: '草种质', value: '草种质'},
            {label: '耕地', value: '耕地'},
            {label: '林地', value: '林地'},
            {label: '全部', value: ''}
        ];
    }

    getContentTypeOptions() {
        return [
            {label: '图片', value: 'image'},
            {label: '文档', value: 'office'},
            {label: '压缩包', value: 'zip'},
            {label: '安装包', value: 'apk'},
            {label: '其他', value: 'other'},
            {label: '全部', value: ''},
        ];
    }

    getImgHost() {
        return this.configSvc.imgHostname;
    }

    getDefaultImgByType(contentType: string) { // 通过文件类型获取默认图标
        const urlMap = {
            zip: '/compression.ico',
            apk: '/setup.ico',
            video: '/video.ico',
            office: '/document.ico',
            other: '/other.ico'
        };
        // TODO 找各个类型的默认图片放到本地
        return this.imgPrefix + (urlMap[contentType] || '/other.ico');
    }

    delete(ids: string | Array<string>): Promise<any> {
        let idsParam = '';
        if (Array.isArray(ids)) {
            ids.map((item) => {
                idsParam += item;
            })
        } else {
            idsParam = ids;
        }
        return this.http.delete('/files', {ids: idsParam});
    }

    deleteBatched(ids) {

    }

    download(path: string) {
        const eleLink = document.createElement('a');
        eleLink.setAttribute('target', '_Blank');
        eleLink.download = '';
        eleLink.style.display = 'none';
        eleLink.href = this.configSvc.imgHostname + path + '?contentDisposition=attachment';
        document.body.appendChild(eleLink);
        eleLink.click();
        document.body.removeChild(eleLink);
    }
}

